﻿////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	Convert1
//	converte i files *.shp e *.dbf i files *.txt (mappe del Veneto)
//  non ancora completato
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;
using System.Drawing;

namespace Map_Fvg
{
    public partial class convert
    {

         public void convert_dbf2(string fileName, string target)
        {
            int ipos = fileName.IndexOf('.');
            string ss = fileName.Substring(0, ipos);
            string file_r = string.Concat(ss, ".dbf");

            string path = FormIniz.quadroUnione5000;
            string dbf = string.Concat(path, target, "_dbf.txtx");
            int iret = convertDbf3_txt(file_r, dbf);
            if (iret != 0) MessageBox.Show("errore:  convertDbf_txt  " + iret.ToString());
        }

        private int convertDbf3_txt(string file_r, string file_w)
        {
            int ifile = 0;

            if (file_w.Contains("curve")) ifile = 1;
            if (file_w.Contains("testi")) ifile = 11;
            if (file_w.Contains("aree_ter")) ifile = 21;
            if (file_w.Contains("veget_a")) ifile = 22;
            if (file_w.Contains("punti_q")) ifile = 31;
            if (file_w.Contains("astevia")) ifile = 2;
            if (file_w.Contains("idrograf")) ifile = 2;
            if (file_w.Contains("discont")) ifile = 2;
            if (file_w.Contains("asteflu")) ifile = 2;
            if (file_w.Contains("viabilit")) ifile = 2;
           if (ifile == 0) return (1);

            if (File.Exists(file_w))
            {
                File.Delete(file_w);
            }


            switch (ifile)
            {

                case 1: curve_dbf(file_r, file_w);
                    break;
                case 2: astevia_dbf(file_r, file_w);
                    break;
                case 11: testi_dbf(file_r, file_w);
                    break;
                case 21: aree_terr_dbf(file_r, file_w);
                    break;
                case 22: veget_a_dbf(file_r, file_w);
                    break;
                case 31: punti_q_dbf(file_r, file_w);
                    break;
                default:
                    MessageBox.Show(ifile.ToString() + "   opzione non disponibile  ");
                    break;
            }

            return (0);
        }



        private int curve_dbf(string file_r, string file_w)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            StreamWriter fs = new StreamWriter(file_w);
            if (File.Exists(file_r))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(file_r, FileMode.Open)))
                {
                    byte bt = reader.ReadByte();        //  numero versione
                    byte[] bts = reader.ReadBytes(3);       //data ultima modifica
                    int numRec = reader.ReadInt32();
                    string ss = "numero records: ";
                    ss = string.Concat(ss, numRec.ToString(), "\n");
                    fs.Write(ss);
                    int lengthHeader = reader.ReadInt16();  //  length of header structure
                    int lengthRec = reader.ReadInt16();  //  length of each record + 1
                    reader.ReadBytes(20);
                    int no_fields = (lengthHeader - 33) / 32;
                    for (int i = 0; i < no_fields; ++i)
                    {
                        int j;
                        bts = reader.ReadBytes(11);
                        for (j = 0; j < 11; ++j)
                        {
                            if (bts[j] == 0) break;
                        }
                        byte[] bte = new byte[j];
                        Buffer.BlockCopy(bts, 0, bte, 0, j);

                        ss = System.Text.Encoding.UTF7.GetString(bte);

                        //           MessageBox.Show(j.ToString() + "  " + ss.Length.ToString());

                        vecT[i].name = string.Copy(ss);

                        bt = reader.ReadByte();
                        vecT[i].type = (char)bt;
                        int fieldDataAddress = reader.ReadInt32();
                        byte fieldlength = reader.ReadByte();
                        vecT[i].length = fieldlength;
                        byte decimalCount = reader.ReadByte();
                        vecT[i].dec = decimalCount;

                        reader.ReadBytes(14);
                        //                                      MessageBox.Show(i.ToString() + "  " + vecT[i].name + "  " + vecT[i].type.ToString() + "  " + vecT[i].length.ToString() + "  " + vecT[i].dec.ToString()); ;
                    }

                    reader.ReadBytes(1);

                    for (int i = 0; i < numRec; ++i)
                    {
                        string ss1 = string.Empty;
                        string ss2 = string.Empty;
                        string ss3 = string.Empty;
                        string ss4 = string.Empty;

                        bt = reader.ReadByte();
                        byte[] bts1 = reader.ReadBytes(lengthRec - 1);
                        int nWidth;
                        int ipnt = 0;


                        for (int j = 0; j < no_fields; ++j)
                        {

                            if (j == 0)         //  NUMERO
                            {
                                nWidth = 11;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, 0, bte, 0, nWidth);
                                ss1 = System.Text.Encoding.UTF7.GetString(bte);
                                int inum = int.Parse(ss1, nfi);
                                ss3 = inum.ToString();
                                ss3 = ss3.PadLeft(10, ' ');
                                ss1 = string.Concat(ss3, "# ");
                                ipnt = nWidth;
                                continue;
                            }

                            if (j == 1)     //      NUME_E_S
                            {
                                nWidth = 6;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 2)     //      DESCRZ
                            {
                                nWidth = 60;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 3)     //      LIV_COD
                            {
                                nWidth = 20;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 4 || j == 5)     //  COD_FACC  ||   ATT_FACC
                            {
                                nWidth = 10;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 6)     //  DATA_CRE
                            {
                                nWidth = 11;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                int inum = int.Parse(ss2, nfi);
                                ss3 = inum.ToString();
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 7)     //  DATA_MOD
                            {
                                nWidth = 11;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 8 || j == 9)     //  TYPE_MOD   || FLG_COUNT
                            {
                                nWidth = 6;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 10)        //  QUOTA
                            {
                                nWidth = 15;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = Encoding.UTF7.GetString(bte);
                                float flt = float.Parse(ss2, nfi);
                                ss3 = flt.ToString("F2", nfi);
                                ss3 = ss3.PadLeft(10, ' ');
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 11)     //  FUS_ORIG
                            {
                                nWidth = 1;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 12)        //  COD_VIS
                            {
                                nWidth = 1;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = Encoding.UTF7.GetString(bte);
                                ss1 = string.Concat(ss1, ss2, "#");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 13 || j == 14)      //  COD_LOT   ||  FUS_ORIG
                            {

                            }

                        }
                           ss1 = string.Concat(ss1, "\n");
                            fs.Write(ss1);
                    }
                    reader.Close();

                    fs.Close();

                }

            }
            return (0);
        }

        private int astevia_dbf(string file_r, string file_w)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            StreamWriter fs = new StreamWriter(file_w);
            if (File.Exists(file_r))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(file_r, FileMode.Open)))
                {
                    byte bt = reader.ReadByte();        //  numero versione
                    byte[] bts = reader.ReadBytes(3);       //data ultima modifica
                    int numRec = reader.ReadInt32();
                    string ss = "numero records: ";
                    ss = string.Concat(ss, numRec.ToString(), "\n");
                    fs.Write(ss);
                    int lengthHeader = reader.ReadInt16();  //  length of header structure
                    int lengthRec = reader.ReadInt16();  //  length of each record + 1
                    reader.ReadBytes(20);
                    int no_fields = (lengthHeader - 33) / 32;
                    for (int i = 0; i < no_fields; ++i)
                    {
                        int j;
                        bts = reader.ReadBytes(11);
                        for (j = 0; j < 11; ++j)
                        {
                            if (bts[j] == 0) break;
                        }
                        byte[] bte = new byte[j];
                        Buffer.BlockCopy(bts, 0, bte, 0, j);

                        ss = System.Text.Encoding.UTF7.GetString(bte);

                        //           MessageBox.Show(j.ToString() + "  " + ss.Length.ToString());

                        vecT[i].name = string.Copy(ss);

                        bt = reader.ReadByte();
                        vecT[i].type = (char)bt;
                        int fieldDataAddress = reader.ReadInt32();
                        byte fieldlength = reader.ReadByte();
                        vecT[i].length = fieldlength;
                        byte decimalCount = reader.ReadByte();
                        vecT[i].dec = decimalCount;

                        reader.ReadBytes(14);
                        //                                      MessageBox.Show(i.ToString() + "  " + vecT[i].name + "  " + vecT[i].type.ToString() + "  " + vecT[i].length.ToString() + "  " + vecT[i].dec.ToString()); ;
                    }

                    reader.ReadBytes(1);

                    for (int i = 0; i < numRec; ++i)
                    {
                        string ss1 = string.Empty;
                        string ss2 = string.Empty;
                        string ss3 = string.Empty;
                        string ss4 = string.Empty;

                        bt = reader.ReadByte();
                        byte[] bts1 = reader.ReadBytes(lengthRec - 1);
                        int nWidth;
                        int ipnt = 0;


                        for (int j = 0; j < no_fields; ++j)
                        {

                            if (j == 0)         //  NUMERO
                            {
                                nWidth = 11;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, 0, bte, 0, nWidth);
                                ss1 = System.Text.Encoding.UTF7.GetString(bte);
                                int inum = int.Parse(ss1, nfi);
                                ss3 = inum.ToString();
                                ss3 = ss3.PadLeft(10, ' ');
                                ss1 = string.Concat(ss3, "# ");
                                ipnt = nWidth;
                                continue;
                            }

                            if (j == 1)     //      NUME_E_S
                            {
                                nWidth = 6;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 2)     //      DESCRZ
                            {
                                nWidth = 60;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 3)     //      LIV_COD
                            {
                                nWidth = 20;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 4 || j == 5)     //  COD_FACC  ||   ATT_FACC
                            {
                                nWidth = 10;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 6)     //  DATA_CRE
                            {
                                nWidth = 11;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 7)     //  DATA_MOD
                            {
                                nWidth = 11;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 8 || j == 9)     //  TYPE_MOD   || FLG_COUNT
                            {
                                nWidth = 6;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 10)        //  QUOTA
                            {
                                nWidth = 15;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 11)     //  FUS_ORIG
                            {
                                nWidth = 1;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 12)        //  COD_VIS
                            {
                                nWidth = 1;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 13 || j == 14)      //  COD_LOT   ||  FUS_ORIG
                            {

                            }

                        }
                        ss1 = string.Concat(ss1, "\n");
                        fs.Write(ss1);
                    }
                    reader.Close();

                    fs.Close();

                }

            }
            return (0);
        }

        private int testi_dbf(string file_r, string file_w)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            StreamWriter fs = new StreamWriter(file_w);
            if (File.Exists(file_r))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(file_r, FileMode.Open)))
                {
                    byte bt = reader.ReadByte();        //  numero versione
                    byte[] bts = reader.ReadBytes(3);       //data ultima modifica
                    int numRec = reader.ReadInt32();
                    string ss = "numero records: ";
                    ss = string.Concat(ss, numRec.ToString(), "\n");
                    fs.Write(ss);
                    int lengthHeader = reader.ReadInt16();  //  length of header structure
                    int lengthRec = reader.ReadInt16();  //  length of each record + 1
                    reader.ReadBytes(20);
                    int no_fields = (lengthHeader - 33) / 32;
                    for (int i = 0; i < no_fields; ++i)
                    {
                        int j;
                        bts = reader.ReadBytes(11);
                        for (j = 0; j < 11; ++j)
                        {
                            if (bts[j] == 0) break;
                        }
                        byte[] bte = new byte[j];
                        Buffer.BlockCopy(bts, 0, bte, 0, j);

                        ss = System.Text.Encoding.UTF7.GetString(bte);

                        //           MessageBox.Show(j.ToString() + "  " + ss.Length.ToString());

                        vecT[i].name = string.Copy(ss);

                        bt = reader.ReadByte();
                        vecT[i].type = (char)bt;
                        int fieldDataAddress = reader.ReadInt32();
                        byte fieldlength = reader.ReadByte();
                        vecT[i].length = fieldlength;
                        byte decimalCount = reader.ReadByte();
                        vecT[i].dec = decimalCount;

                        reader.ReadBytes(14);
                        //                                      MessageBox.Show(i.ToString() + "  " + vecT[i].name + "  " + vecT[i].type.ToString() + "  " + vecT[i].length.ToString() + "  " + vecT[i].dec.ToString()); ;
                    }

                    reader.ReadBytes(1);

                    for (int i = 0; i < numRec; ++i)
                    {
                        string ss1 = string.Empty;
                        string ss2 = string.Empty;
                        string ss3 = string.Empty;
                        string ss4 = string.Empty;

                        bt = reader.ReadByte();
                        byte[] bts1 = reader.ReadBytes(lengthRec - 1);
                        int nWidth;
                        int ipnt = 0;

                        for (int j = 0; j < no_fields; ++j)
                        {
                            if (j == 0)         //  AREA
                            {
                                nWidth = 20;
                                ss1 = string.Empty;
                                ipnt = nWidth;
                                continue;

                            }
                            if (j == 1)         //  PERIMETER
                            {
                                nWidth = 20;
                                ipnt += nWidth;
                                continue;

                            }
                            if (j == 2)         //  TESTI_
                            {
                                nWidth = 11;
                                ipnt += nWidth;
                                continue;

                            }
                            if (j == 3)         //  TESTI_ID
                            {
                                nWidth = 11;
                                ipnt += nWidth;
                                continue;

                            }
                            if (j == 4)         //  NUMERO
                            {
                                nWidth = 11;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                int inum = int.Parse(ss2, nfi);
                                ss3 = inum.ToString();
                                ss3 = ss3.PadLeft(10, ' ');
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;

                            }
                            if (j == 5)     //      NUME_E_S
                            {
                                nWidth = 6;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                ss1 = string.Concat(ss1, ss2, "# ");
                                ipnt += nWidth;
                                continue;

                            }
                            if (j == 6)     //      DESCRZ
                            {
                                nWidth = 60;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                ss1 = string.Concat(ss1, ss2, "# ");
                                ipnt += nWidth;
                                continue;

                            }
                            if (j == 7)     //      LIV_COD
                            {
                                nWidth = 20;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 8 || j == 9)     //  COD_FACC  ||   ATT_FACC
                            {
                                nWidth = 10;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 10)     //  DATA_CRE
                            {
                                nWidth = 11;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                int inum = int.Parse(ss2, nfi);
                                ss3 = inum.ToString();
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 11)     //  DATA_MOD
                            {
                                nWidth = 11;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 12)     //  TYPE_MOD   
                            {
                                nWidth = 6;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 13)        //  TESTO
                            {
                                nWidth = 80;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss3 = Encoding.UTF7.GetString(bte);
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 14)        //  ANG_ROT
                            {
                                nWidth = 20;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = Encoding.UTF7.GetString(bte);
                                float flt = float.Parse(ss2, nfi);
                                ss3 = flt.ToString("F2", nfi);
                                ss3 = ss3.PadLeft(10, ' ');
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 15 || j == 16 || j == 17)      //  FUS_ORIG  ||  COD_LOT   ||  ORIGINE
                            {

                            }
                        }
                        ss1 = string.Concat(ss1, "\n");
                        fs.Write(ss1);
                    }
                    reader.Close();

                    fs.Close();

                }

            }
            return (0);
        }


        private int aree_terr_dbf(string file_r, string file_w)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            StreamWriter fs = new StreamWriter(file_w);
            if (File.Exists(file_r))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(file_r, FileMode.Open)))
                {
                    byte bt = reader.ReadByte();        //  numero versione
                    byte[] bts = reader.ReadBytes(3);       //data ultima modifica
                    int numRec = reader.ReadInt32();
                    string ss = "numero records: ";
                    ss = string.Concat(ss, numRec.ToString(), "\n");
                    fs.Write(ss);
                    int lengthHeader = reader.ReadInt16();  //  length of header structure
                    int lengthRec = reader.ReadInt16();  //  length of each record + 1
                    reader.ReadBytes(20);
                    int no_fields = (lengthHeader - 33) / 32;
                    for (int i = 0; i < no_fields; ++i)
                    {
                        int j;
                        bts = reader.ReadBytes(11);
                        for (j = 0; j < 11; ++j)
                        {
                            if (bts[j] == 0) break;
                        }
                        byte[] bte = new byte[j];
                        Buffer.BlockCopy(bts, 0, bte, 0, j);

                        ss = System.Text.Encoding.UTF7.GetString(bte);

                        //           MessageBox.Show(j.ToString() + "  " + ss.Length.ToString());

                        vecT[i].name = string.Copy(ss);

                        bt = reader.ReadByte();
                        vecT[i].type = (char)bt;
                        int fieldDataAddress = reader.ReadInt32();
                        byte fieldlength = reader.ReadByte();
                        vecT[i].length = fieldlength;
                        byte decimalCount = reader.ReadByte();
                        vecT[i].dec = decimalCount;

                        reader.ReadBytes(14);
                        //                                      MessageBox.Show(i.ToString() + "  " + vecT[i].name + "  " + vecT[i].type.ToString() + "  " + vecT[i].length.ToString() + "  " + vecT[i].dec.ToString()); ;
                    }

                    reader.ReadBytes(1);

                    for (int i = 0; i < numRec; ++i)
                    {
                        string ss1 = string.Empty;
                        string ss2 = string.Empty;
                        string ss3 = string.Empty;
                        string ss4 = string.Empty;

                        bt = reader.ReadByte();
                        byte[] bts1 = reader.ReadBytes(lengthRec - 1);
                        int nWidth;
                        int ipnt = 0;

                        for (int j = 0; j < no_fields; ++j)
                        {
                            if (j == 0)         //  NUMERO
                            {
                                nWidth = 11;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                int inum = int.Parse(ss2, nfi);
                                ss3 = inum.ToString();
                                ss3 = ss3.PadLeft(10, ' ');
                                ss1 = string.Concat(ss3, "# ");
                                ipnt = nWidth;
                                continue;
                            }
                            if (j == 1)         //  NUME_E_S
                            {
                                nWidth = 6;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                ss1 = string.Concat(ss1, ss2,"# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 2)         //  DESCRZ
                            {
                                nWidth = 60;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                ss1 = string.Concat(ss1, ss2, "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 3)         //  LIV_COD
                            {
                                nWidth = 20;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                ss1 = string.Concat(ss1, ss2, "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 4 || j == 5)         //  COD_FACC  ||   ATT_FACC
                            {
                                nWidth = 10;
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 6)     //  DATA_CRE
                            {
                                nWidth = 11;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                int inum = int.Parse(ss2, nfi);
                                ss3 = inum.ToString();
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 7)     //  DATA_MOD
                            {
                                nWidth = 11;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 8)     //  TYPE_MOD   
                            {
                                nWidth = 6;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 9)     //  FLG_CONT
                            {
                                nWidth = 6;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 10)     //  NOME
                            {
                                nWidth = 60;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 11)     // SUPERFIC
                            {
                                nWidth = 15;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 12)     //  PERIMETR
                            {
                                nWidth = 15;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 13 || j == 14 || j == 15  || j == 16)      //  FUS_ORIG  ||  cod_vis  ||  COD_LOT   ||  ORIGINE
                            {

                            }
                        }
                        ss1 = string.Concat(ss1, "\n");
                        fs.Write(ss1);
                    }
                    reader.Close();
                    fs.Close();
                }

            }
            return (0);
        }

        private int veget_a_dbf(string file_r, string file_w)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            StreamWriter fs = new StreamWriter(file_w);
            if (File.Exists(file_r))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(file_r, FileMode.Open)))
                {
                    byte bt = reader.ReadByte();        //  numero versione
                    byte[] bts = reader.ReadBytes(3);       //data ultima modifica
                    int numRec = reader.ReadInt32();
                    string ss = "numero records: ";
                    ss = string.Concat(ss, numRec.ToString(), "\n");
                    fs.Write(ss);
                    int lengthHeader = reader.ReadInt16();  //  length of header structure
                    int lengthRec = reader.ReadInt16();  //  length of each record + 1
                    reader.ReadBytes(20);
                    int no_fields = (lengthHeader - 33) / 32;
                    for (int i = 0; i < no_fields; ++i)
                    {
                        int j;
                        bts = reader.ReadBytes(11);
                        for (j = 0; j < 11; ++j)
                        {
                            if (bts[j] == 0) break;
                        }
                        byte[] bte = new byte[j];
                        Buffer.BlockCopy(bts, 0, bte, 0, j);

                        ss = System.Text.Encoding.UTF7.GetString(bte);

                        //           MessageBox.Show(j.ToString() + "  " + ss.Length.ToString());

                        vecT[i].name = string.Copy(ss);

                        bt = reader.ReadByte();
                        vecT[i].type = (char)bt;
                        int fieldDataAddress = reader.ReadInt32();
                        byte fieldlength = reader.ReadByte();
                        vecT[i].length = fieldlength;
                        byte decimalCount = reader.ReadByte();
                        vecT[i].dec = decimalCount;

                        reader.ReadBytes(14);
   //                                                           MessageBox.Show(i.ToString() + "  " + vecT[i].name + "  " + vecT[i].type.ToString() + "  " + vecT[i].length.ToString() + "  " + vecT[i].dec.ToString()); ;
                    }
                    
                    reader.ReadBytes(1);

                    for (int i = 0; i < numRec; ++i)
                    {
                        string ss1 = string.Empty;
                        string ss2 = string.Empty;
                        string ss3 = string.Empty;
                        string ss4 = string.Empty;

                        bt = reader.ReadByte();
                        byte[] bts1 = reader.ReadBytes(lengthRec - 1);
                        int nWidth;
                        int ipnt = 0;

                        for (int j = 0; j < no_fields; ++j)
                        {

                            if (j == 0)         //  NUMERO
                            {
                                nWidth = 11;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, 0, bte, 0, nWidth);
                                ss1 = System.Text.Encoding.UTF7.GetString(bte);
                                int inum = int.Parse(ss1, nfi);
                                ss3 = inum.ToString();
                                ss3 = ss3.PadLeft(10, ' ');
                                ss1 = string.Concat(ss3, "# ");
                                ipnt = nWidth;
                                continue;
                            }

                            if (j == 1)     //      NUME_E_S
                            {
                                nWidth = 6;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 2)     //      DESCRZ
                            {
                                nWidth = 60;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 3)     //      LIV_COD
                            {
                                nWidth = 20;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j >= 4)
                                break;
                        }
                        ss1 = string.Concat(ss1, "\n");
                        fs.Write(ss1);
                    }
                    reader.Close();
                    fs.Close();
                }

            }
            return (0);
        }


        private int punti_q_dbf(string file_r, string file_w)
        {
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            StreamWriter fs = new StreamWriter(file_w);
            if (File.Exists(file_r))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(file_r, FileMode.Open)))
                {
                    byte bt = reader.ReadByte();        //  numero versione
                    byte[] bts = reader.ReadBytes(3);       //data ultima modifica
                    int numRec = reader.ReadInt32();
                    string ss = "numero records: ";
                    ss = string.Concat(ss, numRec.ToString(), "\n");
                    fs.Write(ss);
                    int lengthHeader = reader.ReadInt16();  //  length of header structure
                    int lengthRec = reader.ReadInt16();  //  length of each record + 1
                    reader.ReadBytes(20);
                    int no_fields = (lengthHeader - 33) / 32;
                    for (int i = 0; i < no_fields; ++i)
                    {
                        int j;
                        bts = reader.ReadBytes(11);
                        for (j = 0; j < 11; ++j)
                        {
                            if (bts[j] == 0) break;
                        }
                        byte[] bte = new byte[j];
                        Buffer.BlockCopy(bts, 0, bte, 0, j);

                        ss = System.Text.Encoding.UTF7.GetString(bte);

                        //           MessageBox.Show(j.ToString() + "  " + ss.Length.ToString());

                        vecT[i].name = string.Copy(ss);

                        bt = reader.ReadByte();
                        vecT[i].type = (char)bt;
                        int fieldDataAddress = reader.ReadInt32();
                        byte fieldlength = reader.ReadByte();
                        vecT[i].length = fieldlength;
                        byte decimalCount = reader.ReadByte();
                        vecT[i].dec = decimalCount;

                        reader.ReadBytes(14);
                        //                                      MessageBox.Show(i.ToString() + "  " + vecT[i].name + "  " + vecT[i].type.ToString() + "  " + vecT[i].length.ToString() + "  " + vecT[i].dec.ToString()); ;
                    }

                    reader.ReadBytes(1);

                    for (int i = 0; i < numRec; ++i)
                    {
                        string ss1 = string.Empty;
                        string ss2 = string.Empty;
                        string ss3 = string.Empty;
                        string ss4 = string.Empty;

                        bt = reader.ReadByte();
                        byte[] bts1 = reader.ReadBytes(lengthRec - 1);
                        int nWidth;
                        int ipnt = 0;

                        for (int j = 0; j < no_fields; ++j)
                        {
                            if (j == 0)         //  AREA
                            {
                                nWidth = 20;
                                ss1 = string.Empty;
                                ipnt = nWidth;
                                continue;
                            }
                            if (j == 1)         //  PERIMETER
                            {
                                nWidth = 20;
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 2)         //  PUNTI_Q_N
                            {
                                nWidth = 11;
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 3)         //  PUNTI_Q_ID
                            {
                                nWidth = 11;
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 4)         //  NUMERO
                            {
                                nWidth = 11;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                int inum = int.Parse(ss2, nfi);
                                ss3 = inum.ToString();
                                ss3 = ss3.PadLeft(10, ' ');
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 5)     //      NUME_E_S
                            {
                                nWidth = 6;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                ss1 = string.Concat(ss1, ss2, "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 6)     //      DESCRZ
                            {
                                nWidth = 60;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                ss1 = string.Concat(ss1, ss2, "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 7)     //      LIV_COD
                            {
                                nWidth = 20;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss1 = string.Concat(ss1, System.Text.Encoding.UTF7.GetString(bte), "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 8 || j == 9)     //  COD_FACC  ||   ATT_FACC
                            {
                                nWidth = 10;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 10)     //  DATA_CRE
                            {
                                nWidth = 11;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = System.Text.Encoding.UTF7.GetString(bte);
                                int inum = int.Parse(ss2, nfi);
                                ss3 = inum.ToString();
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 11)     //  DATA_MOD
                            {
                                nWidth = 11;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 12)     //  TYPE_MOD   
                            {
                                nWidth = 6;
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 13)        //  QUOTA
                            {
                                nWidth = 20;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss3 = Encoding.UTF7.GetString(bte);
                                float inum = float.Parse(ss3, nfi);
                                ss3 = inum.ToString("F2", nfi);
                                ss3 = ss3.PadLeft(10, ' ');
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;
                            }
                            if (j == 14)        //  ANG_ROT
                            {
                                nWidth = 20;
                                byte[] bte = new byte[nWidth];
                                Buffer.BlockCopy(bts1, ipnt, bte, 0, nWidth);
                                ss2 = Encoding.UTF7.GetString(bte);
                                float flt = float.Parse(ss2, nfi);
                                ss3 = flt.ToString("F2", nfi);
                                ss3 = ss3.PadLeft(10, ' ');
                                ss1 = string.Concat(ss1, ss3, "# ");
                                ipnt += nWidth;
                                continue;
                            }

                            if (j == 15 || j == 16 || j == 17)      //  FUS_ORIG  ||  COD_LOT   ||  ORIGINE
                            {

                            }
                        }
                        ss1 = string.Concat(ss1, "\n");
                        fs.Write(ss1);
                    }
                    reader.Close();

                    fs.Close();

                }

            }
            return (0);
        }

        public int compactFiles()
        {
            int iret;
            int nrec, ntot;

            nPoints = 0;
            iret = linea1_dbf();
            if (iret != 0) goto L90;
            iret = linea2_dbf();
            if (iret != 0) goto L90;
            iret = linea3_dbf();
            if (iret != 0) goto L90;
            iret = linea4_dbf();
            if (iret != 0) goto L90;
            iret = linea5_dbf();
            if (iret != 0) goto L90;
            iret = linea6_dbf();
            if (iret != 0) goto L90;

            iret = linea_dbf();
            if (iret != 0) goto L90;  
    
            iret = linea1_shp();
            if (iret != 0) goto L90;
            iret = linea_shp();
            if (iret != 0) goto L90;
         
            iret = text_dbf();
            if (iret != 0) goto L90;
            iret = text_shp();
            if (iret != 0) goto L90;

            ntot = 0;
            iret = 1;
            nrec = area1_dbf();
            if (nrec < 0) goto L90;
            ntot += nrec;
            nrec = area2_dbf();
            if (nrec < 0) goto L90;
            ntot += nrec;
            nrec = area_dbf(ntot);
            if (nrec < 0) goto L90;

            iret = area1_shp();
            if (iret < 0) goto L90;
            iret = area_shp();
            if (iret < 0) goto L90;

            iret = point_dbf();
            if (iret != 0) goto L90;
            iret = point_shp();
            if (iret != 0) goto L90;
        L90: if (iret > 0)
            {
                MessageBox.Show("iret == " + iret.ToString());
                return (iret);
            }
       
            string path = FormIniz.quadroUnione5000;
            string[] fileEntries= Directory.GetFileSystemEntries(path, "*.txtx");
            foreach (string fileName in fileEntries)
            {
   //                         File.Delete(fileName);
            }

            int exec = 0;
            string fileNameX = string.Concat(FormIniz.cartellaElementi_isolati, "047012.zip");
        
           if (!File.Exists(fileNameX)) ++exec;
            if (exec >= 0)
            {

                Zip.Extract1 Extr = new Zip.Extract1();
             //                              Zip.Trace.Open("Zipfile1.txt");
                string path1 = FormIniz.quadroUnione5000;

                iret = Extr.CompressToFile(path1, fileNameX);
            }
            else MessageBox.Show("exec == 0");
                 
       
            return (iret);
        }

        static private int nPoints, nShapeT;

        private int linea1_dbf()
        {

            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars = { ':', ',' };
            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };

            string file_r = string.Concat(path, "curve_dbf.txtx");
            string file_w = string.Concat(path, "line_dbf.txtx");
            string[] words;
            string s4 = string.Empty;
            string s5 = string.Empty;
            string ss = string.Empty;

            if (File.Exists(file_w)) File.Delete(file_w);

            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = new StreamWriter(file_w);

                s4 = fr.ReadLine();
                s4 = string.Concat(s4, "\n");
                fw.Write(s4);

                words = s4.Split(delimiterChars20);
                int nrec = int.Parse(words[1]);
                for (int j = 0; j < nrec; ++j)
                {
                    s4 = fr.ReadLine();
                    words = s4.Split(delimiterChars2);
                    //           ss = j.ToString();
                    ss = string.Copy(words[0]);
                    ss = ss.PadLeft(6, ' ');
                    ss = string.Concat(ss, "#");

                    if (words[3].Contains("1001"))          //  DENOMINAZIONE
                    {
                        s5 = "1L000CD   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "CURVA DI LIVELLO DIRETTRICE";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                    }

                    if (words[3].Contains("1002"))
                    {
                        s5 = "1L000CI   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "CURVA DI LIVELLO INTERMEDIA";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                    }

                    if (words[3].Contains("1003"))
                    {
                        s5 = "1L000CA   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "CURVA DI LIVELLO AUSILIARIA";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                    }

                    s5 = "OROGRAFIA";             //      CLASSE
                    s5 = s5.PadRight(20, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[1];             //      ELEMENTO
                    s5 = s5.PadRight(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "1";             //      EDIZIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "0.0";             //      LUNGHEZZA
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[5];              //      QUOTA
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    ss = string.Concat(ss, "\n");
                    fw.Write(ss);
                }

                fr.Close();
                fw.Close();
                nPoints = nrec;

                File.Delete(file_r);
            }

            return (0);
        }

        private int linea2_dbf()
        {
            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars = { ':', ',' };
            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };

            string file_r = string.Concat(path, "astevia_dbf.txtx");
            string file_w = string.Concat(path, "line_dbf.txtx");
            string[] words;

            string s4 = string.Empty;
            string s5 = string.Empty;
            string ss = string.Empty;

            int nrec = -1;
            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = File.AppendText(file_w);

                s4 = fr.ReadLine();
                words = s4.Split(delimiterChars20);
                nrec = int.Parse(words[1]);

                s4 = string.Concat(words[0], ": ", nrec.ToString(), "\n");
                fw.Write(s4);

                for (int j = 0; j < nrec; ++j)
                {
                    s4 = fr.ReadLine();
                    words = s4.Split(delimiterChars2);

                    ss = string.Empty;

                    s5 = words[0];              //      ID
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, "#");

                    if (words[3].Contains("A217"))
                    {
                        s5 = "5L000SE   ";                  //  CODICE
                        ss = string.Concat(ss, s5, "#");
                        s5 = "SENTIERO";                      //  DENOMINAZIONE
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                    }

                    s5 = "VIABILITA";             //      CLASSE
                    s5 = s5.PadRight(20, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[1];             //      ELEMENTO
                    s5 = s5.PadRight(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "1";             //      EDIZIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, " #");

                    s5 = "0.0";             //      AREA
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, " #");

                    s5 = "0.0";              //      MEDIA_Q
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, " #");

                    ss = string.Concat(ss, "\n");
                    fw.Write(ss);
                }

                fr.Close();
                fw.Close();
            }
            nPoints += nrec;

            File.Delete(file_r);

            return (0);
        }

        private int linea3_dbf()
        {
            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars = { ':', ',' };
            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };

            string file_r = string.Concat(path, "idrograf_dbf.txtx");
            string file_w = string.Concat(path, "line_dbf.txtx");
            string[] words;

            string s4 = string.Empty;
            string s5 = string.Empty;
            string ss = string.Empty;

            int nrec = -1;
            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = File.AppendText(file_w);

                s4 = fr.ReadLine();
                words = s4.Split(delimiterChars20);
                nrec = int.Parse(words[1]);

                s4 = string.Concat(words[0], ": ", nrec.ToString(), "\n");
                fw.Write(s4);

                for (int j = 0; j < nrec; ++j)
                {
                    s4 = fr.ReadLine();
                    words = s4.Split(delimiterChars2);

                    ss = string.Empty;

                    s5 = words[0];              //      ID
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, "#");

                    int ifound = 0;
                    if (words[3].Contains("0401"))
                    {
                        s5 = "2L000CR   ";                  //  CODICE
                        ss = string.Concat(ss, s5, "#");
                        s5 = "CORSO D'ACQUA ";                      //  DENOMINAZIONE
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        ifound = 1;
                    }
                    if (words[3].Contains("0414"))
                    {
                        s5 = "2L000AC   ";                  //  CODICE
                        ss = string.Concat(ss, s5, "#");
                        s5 = "ACQUEDOTTO";                      //  DENOMINAZIONE
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        ifound = 1;
                    }
                    if (words[3].Contains("0427"))
                    {
                        s5 = "2L000FR   ";                  //  CODICE
                        ss = string.Concat(ss, s5, "#");
                        s5 = "IMPLUVIO DI MONTAGNA";                      //  DENOMINAZIONE
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        ifound = 1;
                    }
                    
                    if (words[3].Contains("0408"))
                    {
                        s5 = "2L000BG   ";                  //  CODICE
                        ss = string.Concat(ss, s5, "#");
                        s5 = "BRIGLIA";                      //  DENOMINAZIONE
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        ifound = 1;
                    }
                    
                    if(ifound == 0)
                    {
                        MessageBox.Show("non esiste corrispondenza con " + words[3]);
                        break;
                    }


                    s5 = "IDROGRAFIA";             //      CLASSE
                    s5 = s5.PadRight(20, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[1];             //      ELEMENTO
                    s5 = s5.PadRight(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "1";             //      EDIZIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, " #");

                    s5 = "0.0";             //      AREA
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, " #");

                    s5 = "0.0";              //      MEDIA_Q
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, " #");

                    ss = string.Concat(ss, "\n");
                    fw.Write(ss);
                }

                fr.Close();
                fw.Close();
            }
            nPoints += nrec;

            File.Delete(file_r);

            return (0);
        }

                private int linea4_dbf()
        {
            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars = { ':', ',' };
            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };

            string file_r = string.Concat(path, "discont_dbf.txtx");
            string file_w = string.Concat(path, "line_dbf.txtx");
            string[] words;

            string s4 = string.Empty;
            string s5 = string.Empty;
            string ss = string.Empty;

            int nrec = -1;
            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = File.AppendText(file_w);

                s4 = fr.ReadLine();
                words = s4.Split(delimiterChars20);
                nrec = int.Parse(words[1]);

                s4 = string.Concat(words[0], ": ", nrec.ToString(), "\n");
                fw.Write(s4);

                for (int j = 0; j < nrec; ++j)
                {
                    s4 = fr.ReadLine();
                    words = s4.Split(delimiterChars2);

                    ss = string.Empty;

                    s5 = words[0];              //      ID
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, "#");

                    int ifound = 0;
                    if (words[3].Contains("0608"))
                    {
                        s5 = "1L000SP   ";                  //  CODICE
                        ss = string.Concat(ss, s5, "#");
                        s5 = "SCARPATA (PIEDE) ";                      //  DENOMINAZIONE
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        ifound = 1;
                    }
                    if (words[3].Contains("2208"))
                    {
                        s5 = "1L000SC   ";                  //  CODICE
                        ss = string.Concat(ss, s5, "#");
                        s5 = "SCARPATA (CIGLIO)";                      //  DENOMINAZIONE
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        ifound = 1;
                    }

                    if (ifound == 0)
                    {
                        MessageBox.Show("non esiste corrispondenza con " + words[3]);
                        break;
                    }


                    s5 = "OROGRAFIA";             //      CLASSE
                    s5 = s5.PadRight(20, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[1];             //      ELEMENTO
                    s5 = s5.PadRight(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "1";             //      EDIZIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, " #");

                    s5 = "0.0";             //      AREA
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, " #");

                    s5 = "0.0";              //      MEDIA_Q
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, " #");

                    ss = string.Concat(ss, "\n");
                    fw.Write(ss);
                }

                fr.Close();
                fw.Close();
            }
            nPoints += nrec;

            File.Delete(file_r);

            return (0);
        }

                private int linea5_dbf()
                {
                    string path = FormIniz.quadroUnione5000;
                    NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

                    char[] delimiterChars = { ':', ',' };
                    char[] delimiterChars20 = { ':', ',', '=' };
                    char[] delimiterChars2 = { '#' };

                    string file_r = string.Concat(path, "asteflu_dbf.txtx");
                    string file_w = string.Concat(path, "line_dbf.txtx");
                    string[] words;

                    string s4 = string.Empty;
                    string s5 = string.Empty;
                    string ss = string.Empty;

                    int nrec = -1;
                    if (File.Exists(file_r))
                    {
                        StreamReader fr = File.OpenText(file_r);
                        StreamWriter fw = File.AppendText(file_w);

                        s4 = fr.ReadLine();
                        words = s4.Split(delimiterChars20);
                        nrec = int.Parse(words[1]);

                        s4 = string.Concat(words[0], ": ", nrec.ToString(), "\n");
                        fw.Write(s4);

                        for (int j = 0; j < nrec; ++j)
                        {
                            s4 = fr.ReadLine();
                            words = s4.Split(delimiterChars2);

                            ss = string.Empty;

                            s5 = words[0];              //      ID
                            s5 = s5.PadLeft(10, ' ');
                            ss = string.Concat(ss, s5, "#");

                            int ifound = 0;
                            if (words[3].Contains("A401"))
                            {
                                s5 = "2L001CR   ";                  //  CODICE
                                ss = string.Concat(ss, s5, "#");
                                s5 = "CORSO D'ACQUA ";                      //  DENOMINAZIONE
                                s5 = s5.PadRight(80, ' ');
                                ss = string.Concat(ss, s5, "#");
                                ifound = 1;
                            }
                            if (words[3].Contains("A414"))
                            {
                                s5 = "2L001AC   ";                  //  CODICE
                                ss = string.Concat(ss, s5, "#");
                                s5 = "ACQUEDOTTO";                      //  DENOMINAZIONE
                                s5 = s5.PadRight(80, ' ');
                                ss = string.Concat(ss, s5, "#");
                                ifound = 1;
                            }
                            if (words[3].Contains("A427"))
                            {
                                s5 = "2L001FR   ";                  //  CODICE
                                ss = string.Concat(ss, s5, "#");
                                s5 = "IMPLUVIO DI MONTAGNA";                      //  DENOMINAZIONE
                                s5 = s5.PadRight(80, ' ');
                                ss = string.Concat(ss, s5, "#");
                                ifound = 1;
                            }

                            if (ifound == 0)
                            {
                                MessageBox.Show("non esiste corrispondenza con " + words[3]);
                                break;
                            }


                            s5 = "IDROGRAFIA";             //      CLASSE
                            s5 = s5.PadRight(20, ' ');
                            ss = string.Concat(ss, s5, "#");

                            s5 = words[1];             //      ELEMENTO
                            s5 = s5.PadRight(8, ' ');
                            ss = string.Concat(ss, s5, "#");

                            s5 = "1";             //      EDIZIONE
                            s5 = s5.PadLeft(8, ' ');
                            ss = string.Concat(ss, s5, " #");

                            s5 = "0.0";             //      AREA
                            s5 = s5.PadLeft(10, ' ');
                            ss = string.Concat(ss, s5, " #");

                            s5 = "0.0";              //      MEDIA_Q
                            s5 = s5.PadLeft(8, ' ');
                            ss = string.Concat(ss, s5, " #");

                            ss = string.Concat(ss, "\n");
                            fw.Write(ss);
                        }

                        fr.Close();
                        fw.Close();
                    }
                    nPoints += nrec;

                    File.Delete(file_r);

                    return (0);
                }

                private int linea6_dbf()
                {
                    string path = FormIniz.quadroUnione5000;
                    NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

                    char[] delimiterChars = { ':', ',' };
                    char[] delimiterChars20 = { ':', ',', '=' };
                    char[] delimiterChars2 = { '#' };

                    string file_r = string.Concat(path, "viabilit_dbf.txtx");
                    string file_w = string.Concat(path, "line_dbf.txtx");
                    string[] words;

                    string s4 = string.Empty;
                    string s5 = string.Empty;
                    string ss = string.Empty;

                    int nrec = -1;
                    if (File.Exists(file_r))
                    {
                        StreamReader fr = File.OpenText(file_r);
                        StreamWriter fw = File.AppendText(file_w);

                        s4 = fr.ReadLine();
                        words = s4.Split(delimiterChars20);
                        nrec = int.Parse(words[1]);

                        s4 = string.Concat(words[0], ": ", nrec.ToString(), "\n");
                        fw.Write(s4);

                        for (int j = 0; j < nrec; ++j)
                        {
                            s4 = fr.ReadLine();
                            words = s4.Split(delimiterChars2);

                            ss = string.Empty;

                            s5 = words[0];              //      ID
                            s5 = s5.PadLeft(10, ' ');
                            ss = string.Concat(ss, s5, "#");

                            int ifound = 0;
                            if (words[3].Contains("0217"))
                            {
                                s5 = "5L000SE   ";                  //  CODICE
                                ss = string.Concat(ss, s5, "#");
                                s5 = "SENTIERO";                      //  DENOMINAZIONE
                                s5 = s5.PadRight(80, ' ');
                                ss = string.Concat(ss, s5, "#");
                                ifound = 1;
                            }

                            if (ifound == 0)
                            {
                                MessageBox.Show("non esiste corrispondenza con " + words[3]);
                                break;
                            }

                            s5 = "VIABILITA";             //      CLASSE          
                            s5 = s5.PadRight(20, ' ');
                            ss = string.Concat(ss, s5, "#");

                            s5 = words[1];             //      ELEMENTO
                            s5 = s5.PadRight(8, ' ');
                            ss = string.Concat(ss, s5, "#");

                            s5 = "1";             //      EDIZIONE
                            s5 = s5.PadLeft(8, ' ');
                            ss = string.Concat(ss, s5, " #");

                            s5 = "0.0";             //      AREA
                            s5 = s5.PadLeft(10, ' ');
                            ss = string.Concat(ss, s5, " #");

                            s5 = "0.0";              //      MEDIA_Q
                            s5 = s5.PadLeft(8, ' ');
                            ss = string.Concat(ss, s5, " #");

                            ss = string.Concat(ss, "\n");
                            fw.Write(ss);
                        }

                        fr.Close();
                        fw.Close();
                    }
                    nPoints += nrec;

                    File.Delete(file_r);

                    return (0);
                }
        private int linea_dbf()
        {
            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };
            string[] words;

            string file_r = string.Concat(path, "line_dbf.txtx");
            string file_w = string.Concat(path, "line_dbf.txt");
            string s4 = string.Empty;
            string s5 = string.Empty;
            string ss = string.Empty;

            int nrec = 0;
            int iret = 0;

            if (File.Exists(file_w)) File.Delete(file_w);

            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = new StreamWriter(file_w);
                int irec = 0;
                nrec = 0;

                for (int k = 0; k < 6; ++k)
                {
                    s4 = fr.ReadLine();
                    words = s4.Split(delimiterChars20);
                    if(k==0)
                    {
                        s4 = string.Concat("numero records: ", nPoints.ToString(), "\n");
                        fw.Write(s4);
                    }

                    irec = int.Parse(words[1]);
                    for (int j = 0; j < irec; ++j)
                    {
                        s4 = fr.ReadLine();
                        s4 = string.Concat(s4, "\n");
                        fw.Write(s4);
                    }
                    nrec += irec;
                }

                fr.Close();
                fw.Close();

                if (nrec != nPoints)
                {
                    MessageBox.Show("linea_dbf; errore  "+ nrec.ToString() + "  " +nPoints.ToString());
                    iret = -1;
                }
            }

            return (iret);
        }


        private int text_dbf()
        {
            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars = { ':', ',' };
            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };

            string file_r = string.Concat(path, "testi_dbf.txtx");
            string file_w = string.Concat(path, "text_dbf.txt");
            string[] words;
            string s4 = string.Empty;
            string s5 = string.Empty;
            string ss = string.Empty;

            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = new StreamWriter(file_w);

                s4 = fr.ReadLine();
                s4 = string.Concat(s4, "\n");
                fw.Write(s4);

                words = s4.Split(delimiterChars20);
                int nrec = int.Parse(words[1]);
                int classe = 0;
                for (int j = 0; j < nrec; ++j)
                {
                    s4 = fr.ReadLine();
                    words = s4.Split(delimiterChars2);
                    //           ss = j.ToString();
                    ss = string.Copy(words[0]);
                    ss = ss.PadLeft(6, ' ');
                    ss = string.Concat(ss, "#");

                    if (words[3].Contains("1004"))          //  DENOMINAZIONE
                    {
                        s5 = "1T000VD   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "VALORE ALTIMETRICO DI CURVA DIRETTRICE";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        classe = 1;
                    }

                    if (words[3].Contains("1407"))
                    {
                        s5 = "4T000EF   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "TOPONOMASTICA EDIFICI";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        classe = 4;
                    }

                    if (words[3].Contains("1409"))
                    {
                        s5 = "1T000OS   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "TOPONOMASTICA OROGRAFICA SECODARIA";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        classe = 1;
                    }

                    if (words[3].Contains("1410"))
                    {
                        s5 = "1T000DO   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "TOPONOMASTICA OROGRAFIA MINORE";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        classe = 1;
                    }

                    if (words[3].Contains("1416"))
                    {
                        s5 = "2T000TI   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "SCRITTURE PICCOLI CORSI D`ACQUA, FONTANE, ACQUEDOTTI";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        classe = 2;
                    }

                    if (words[3].Contains("1419"))
                    {
                        s5 = "1T000DO   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "TOPONOMASTICA OROGRAFIA MINORE";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        classe = 1;
                    }

                    if (words[3].Contains("1604"))
                    {
                        s5 = "1T000DO   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "TOPONOMASTICA OROGRAFIA MINORE";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                        classe = 1;
                    }




                    switch (classe)      //      CLASSE
                    {
                        case 1:
                            s5 = "OROGRAFIA";
                            break;
                        case 2:
                            s5 = "IDROGRAFIA";
                            break;
                        case 4:
                            s5 = "EDIFICATO";
                            break;
                        case 5:
                            s5 = "VIABILITA";
                            break;
                    }
                    s5 = s5.PadRight(20, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[1];             //      ELEMENTO
                    s5 = s5.PadRight(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "1";             //      EDIZIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[5];             //      TESTO
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "130.0";              //      ALTEZZA
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "0.0";              //      ROTAZIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[5].Trim();
                    int ncar = s5.Length;
                    s5 = ncar.ToString();              //      NCAR_TESTO
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "2";              //      COD_POSIZ
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[6];              //      GMROTATION
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    ss = string.Concat(ss, "\n");
                    fw.Write(ss);
                }

                fr.Close();
                fw.Close();

                File.Delete(file_r);

            }
            return (0);
        }

        private int area1_dbf()
        {
            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };
            string[] words;
            
            string file_r = string.Concat(path, "aree_ter_dbf.txtx");
            string file_w = string.Concat(path, "area_dbf.txtx");
            string s4 = string.Empty;
            string s5 = string.Empty;
            string ss = string.Empty;
            int nrec = -1;

            if (File.Exists(file_w)) File.Delete(file_w);

            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = new StreamWriter(file_w);

                s4 = fr.ReadLine();
                s4 = string.Concat(s4, "\n");
                fw.Write(s4);

                words = s4.Split(delimiterChars20);
                nrec = int.Parse(words[1]);
                for (int j = 0; j < nrec; ++j)
                {
                    s4 = fr.ReadLine();
                    words = s4.Split(delimiterChars2);

                    ss = string.Empty;

                    s5 = words[0];              //      ID
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, "#");

                    if (words[3].Contains("1301"))          
                    {
                        s5 = "1A000RO   ";                  //  CODICE
                        ss = string.Concat(ss, s5, "#");
                        s5 = "ROCCIA";                      //  DENOMINAZIONE
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                    }

                    if (words[3].Contains("1302"))
                    {
                        s5 = "1A000GH   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "GHIAIONI";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                    }

                    s5 = "OROGRAFIA";             //      CLASSE
                    s5 = s5.PadRight(20, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[1];             //      ELEMENTO
                    s5 = s5.PadRight(8, ' ');
                    ss = string.Concat(ss, s5, "#" );

                    s5 = "1";             //      EDIZIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, " #");

                    s5 = "0.0";             //      AREA
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, " # ");

                    s5 = "0.0";              //      MEDIA_Q
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, " #");

                    ss = string.Concat(ss, "\n");
                    fw.Write(ss);
                }

                fr.Close();
                fw.Close();
            }

            File.Delete(file_r);

            return (nrec);
        }


        private int area2_dbf( )
        {
            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars = { ':', ',' };
            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };

            string file_r = string.Concat(path, "veget_a_dbf.txtx");
            string file_w = string.Concat(path, "area_dbf.txtx");
            string[] words;

            string s4 = string.Empty;
            string s5 = string.Empty;
            string ss = string.Empty;

            int nrec = -1;
            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = File.AppendText(file_w);

                s4 = fr.ReadLine();
                words = s4.Split(delimiterChars20);
                nrec = int.Parse(words[1]);

                s4 = string.Concat(words[0],": ",nrec.ToString(), "\n");
                fw.Write(s4);

                for (int j = 0; j < nrec; ++j)
                {
                    s4 = fr.ReadLine();
                    words = s4.Split(delimiterChars2);

                    ss = string.Empty;

                    s5 = words[0];              //      ID
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, "#");

                    if (words[3].Contains("0502 "))
                    {
                        s5 = "3A000LB   ";                  //  CODICE
                        ss = string.Concat(ss, s5, "#");
                        s5 = "LIMITE DI BOSCO, TAGLIATA";                      //  DENOMINAZIONE
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                    }

                    if (words[3].Contains("0503"))
                    {
                        s5 = "3A000LB   "; 
                        ss = string.Concat(ss, s5, "#");
                        s5 = "LIMITE DI BOSCO, TAGLIATA";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                    }

                    if (words[3].Contains("0504"))
                    {
                        s5 = "3A000BC   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "BOSCO";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");
                    }
                    s5 = "VEGETAZIONE";             //      CLASSE
                    s5 = s5.PadRight(20, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[1];             //      ELEMENTO
                    s5 = s5.PadRight(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "1";             //      EDIZIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, " #");

                    s5 = "0.0";             //      AREA
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, " #");

                    s5 = "0.0";              //      MEDIA_Q
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, " #");

                    ss = string.Concat(ss, "\n");
                    fw.Write(ss);
                }

                fr.Close();
                fw.Close();
            }

            File.Delete(file_r);

            return (nrec);
        }

        private int area_dbf(int nrecT)
        {
            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };
            string[] words;

            if(nrecT <= 0) return(-1);

            string file_r = string.Concat(path, "area_dbf.txtx");
            string file_w = string.Concat(path, "area_dbf.txt");
            string s4 = string.Empty;
            string s5 = string.Empty;
            string ss = string.Empty;

            int nrec = -1;

            if (File.Exists(file_w)) File.Delete(file_w);

            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = new StreamWriter(file_w);

                s4 = fr.ReadLine();
                words = s4.Split(delimiterChars20);
                s4 = string.Concat("numero records: ", nrecT.ToString(),"\n");
                fw.Write(s4);

                
                int irec = int.Parse(words[1]);
                for (int j = 0; j < irec; ++j)
                {
                    s4 = fr.ReadLine();
                    s4 = string.Concat(s4, "\n");
                    fw.Write(s4);
                }
 
                nrec = irec;
                s4 = fr.ReadLine();
                words = s4.Split(delimiterChars20);
                irec = int.Parse(words[1]);
                for (int j = 0; j < irec; ++j)
                {
                    s4 = fr.ReadLine();
                    s4 = string.Concat(s4, "\n");
                    fw.Write(s4);
                }
                nrec += irec;
               
                fr.Close();
                fw.Close();
                if(nrec != nrecT)
                {
                    MessageBox.Show("area_dbf; errore");
                    nrec = -1;
                }
            }

            return (nrec);
        }


        private int point_dbf()
        {
            string path = FormIniz.quadroUnione5000;
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            char[] delimiterChars = { ':', ',' };
            char[] delimiterChars20 = { ':', ',', '=' };
            char[] delimiterChars2 = { '#' };

            string file_r = string.Concat(path, "punti_q_dbf.txtx");
            string file_w = string.Concat(path, "point_dbf.txt");
            string[] words;
            string s4 = string.Empty;
            string s5 = string.Empty;
            string ss = string.Empty;

            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = new StreamWriter(file_w);

                s4 = fr.ReadLine();
                s4 = string.Concat(s4, "\n");
                fw.Write(s4);

                words = s4.Split(delimiterChars20);
                int nrec = int.Parse(words[1]);

                for (int j = 0; j < nrec; ++j)
                {
                    s4 = fr.ReadLine();
                    words = s4.Split(delimiterChars2);
                    //           ss = j.ToString();
                    ss = string.Copy(words[0]);         //  ID
                    ss = ss.PadLeft(10, ' ');
                    ss = string.Concat(ss, "#");

                    if (words[3].Contains("1102"))          //  CODICE
                    {
                        s5 = "1P000QS   ";
                        ss = string.Concat(ss, s5, "#");
                        s5 = "QUOTA STEREO TERRENO";
                        s5 = s5.PadRight(80, ' ');
                        ss = string.Concat(ss, s5, "#");

                        s5 = "OROGRAFIA";
                        s5 = s5.PadRight(20, ' ');
                        ss = string.Concat(ss, s5, "#");
                    }

                    s5 = words[1];             //      ELEMENTO
                    s5 = s5.PadRight(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = "1";             //      EDIZIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[5];             //      QUOTA
                    s5 = s5.PadLeft(10, ' ');
                    ss = string.Concat(ss, s5, "#");


                    s5 = "0.0";              //      ROTAZIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");


                    s5 = "80.0";              //      DIMENSIONE
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    s5 = words[6];              //      GMROTATION
                    s5 = s5.PadLeft(8, ' ');
                    ss = string.Concat(ss, s5, "#");

                    ss = string.Concat(ss, "\n");
                    fw.Write(ss);
                }

                fr.Close();
                fw.Close();

                File.Delete(file_r);

            }

            return (0);
        }


        private int linea1_shp()
        {

            string path = FormIniz.quadroUnione5000;

            string file_r = string.Concat(path, "line_shp.txtx");

            int iret = countPoints(file_r);
            if(iret < 0) goto L90;

            file_r = string.Concat(path, "astevia_shp.txtx");
            string file_w = string.Concat(path, "line_shp.txtx");

            iret = addfile_shp(file_r, file_w);
            if (iret < 0) goto L90;

            file_r = string.Concat(path, "idrograf_shp.txtx");
            iret = addfile_shp(file_r, file_w);
            if (iret < 0) goto L90;

            file_r = string.Concat(path, "discont_shp.txtx");
            iret = addfile_shp(file_r, file_w);
            if (iret < 0) goto L90;

            file_r = string.Concat(path, "asteflu_shp.txtx");
            iret = addfile_shp(file_r, file_w);
            if (iret < 0) goto L90;

            file_r = string.Concat(path, "viabilit_shp.txtx");
            iret = addfile_shp(file_r, file_w);
            if (iret < 0) goto L90;
L90:
            return (iret);
        }

        private int linea_shp()
        {
            int iret = 0;

            string path = FormIniz.quadroUnione5000;
            string file_r = string.Concat(path, "line_shp.txtx");
            string file_w = string.Concat(path, "line_shp.txt");

            int nfile = 6;
            addfile1_shp(nfile, file_r, file_w);

            return (iret);
        }


 

        private int text_shp()
        {
             string path = FormIniz.quadroUnione5000;

            string file_r = string.Concat(path, "text_shp.txtx");
            string file_w = string.Concat(path, "text_shp.txt");

            char[] delimiterChars = { ':', ',', ';' };
            string[] words;


            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = new StreamWriter(file_w);

                string ss = fr.ReadLine();
                words = ss.Split(delimiterChars);

                string s1 = string.Concat("Shapefile type: MultiPointZ  # of Shape: ", words[2], "; # of Points; ", words[2], "\n");
                fw.Write(s1);

                ss = fr.ReadLine();
                ss = string.Concat(ss, "\n");
                fw.Write(ss);

                int nrec = int.Parse(words[2]);
                for (int j = 0; j < nrec; ++j)
                {
                    ss = fr.ReadLine();
                    string s2 = string.Concat("Shape:", j.ToString(), ", MultiPointZ nVertices=1, nParts=0", "\n");
                    fw.Write(s2);
                    ss = fr.ReadLine();
                    words = ss.Split(delimiterChars);
                    s2 = string.Concat("Bounds: ", words[1], " , ", words[2], " , ", words[1], " , ", words[2], "\n");
                    fw.Write(s2);
                    s2 = string.Concat("  ", words[1], " , ", words[2], "\n");
                    fw.Write(s2);
                }

                fr.Close();
                fw.Close();

            }

            return (0);
        }


        private int area1_shp()
        {

            string path = FormIniz.quadroUnione5000;

            string file_r = string.Concat(path, "area_shp.txtx");

            int iret = countPoints(file_r);
            if (iret < 0) goto L90;

            file_r = string.Concat(path, "veget_a_shp.txtx");
            string file_w = string.Concat(path, "area_shp.txtx");
            iret = addfile_shp(file_r, file_w);
            if (iret < 0) goto L90;


        L90:
            return (iret);

        }

  

        private int area_shp()
        {
            string path = FormIniz.quadroUnione5000;
            string file_r = string.Concat(path, "area_shp.txtx");
            string file_w = string.Concat(path, "area_shp.txt");

            int nfile = 2;
            int iret = addfile1_shp(nfile,file_r,file_w);

            return (iret);
        }
            /*
            StreamReader fr = File.OpenText(file_r);
            StreamWriter fw = new StreamWriter(file_w);

            string ss = fr.ReadLine();
            int mpos = ss.IndexOf('#');
            string ss1 = ss.Substring(0, mpos + 1);
            ss1 = string.Concat(ss1, " of Shape: ",nShapeT.ToString(),"; # of Points: ",nrecT.ToString(), "\n");
            fw.Write(ss1);

            words = ss.Split(delimiterChars);
            int nShape = int.Parse(words[2]);

            ss = fr.ReadLine();
            ss = string.Concat(ss, "\n");
            fw.Write(ss);

            for (int i = 0; i < nShape; ++i)
            {
                ss = fr.ReadLine();
                ss = string.Concat(ss, "\n");
                fw.Write(ss);

                int len = ss.Length;
                int ipos = ss.IndexOf('=', 20, len - 20);
                int lpos = 0;
                if (ipos > 0)
                {
                    ++ipos;
                    lpos = ss.IndexOf(',', ipos, len - ipos);
                    string s1 = ss.Substring(ipos, lpos - ipos);
                    int icount = int.Parse(s1);
                    for (int j = 0; j <= icount; ++j)
                    {
                        ss = fr.ReadLine();
                        ss = string.Concat(ss, "\n");
                        fw.Write(ss);
                    }
                 }
            }

            int count = nShape;

            ss = fr.ReadLine();
            words = ss.Split(delimiterChars);
            nShape = int.Parse(words[2]);
            ss = fr.ReadLine();

            for (int i = 0; i < nShape; ++i)
            {
                string ss2 = fr.ReadLine();
                words = ss2.Split(delimiterChars);
                ss = string.Concat(words[0], ":", count.ToString(), ",", words[2], ",", words[3], "\n");
                fw.Write(ss);

                int len = ss.Length;
                int ipos = ss.IndexOf('=', 20, len - 20);
                int lpos = 0;
                if (ipos > 0)
                {
                    ++ipos;
                    lpos = ss.IndexOf(',', ipos, len - ipos);
                    string s1 = ss.Substring(ipos, lpos - ipos);
                    int icount = int.Parse(s1);
                    for (int j = 0; j <= icount; ++j)
                    {
                        ss = fr.ReadLine();
                        ss = string.Concat(ss, "\n");
                        fw.Write(ss);
                    }
                    ++count;
                }
            }


            fr.Close();
            fw.Close();
 */


        private int point_shp()
        {
            string path = FormIniz.quadroUnione5000;
            char[] delimiterChars = { ':', ',',';' };
            string[] words;
            string file_r = string.Concat(path, "point_shp.txtx");
            string file_w = string.Concat(path, "point_shp.txt");

            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = new StreamWriter(file_w);

                string ss = fr.ReadLine();
                words = ss.Split(delimiterChars);

                string s1 = string.Concat("Shapefile type: MultiPointZ  # of Shape: ", words[2], "; # of Points; ", words[2], "\n");
                fw.Write(s1);
       
                ss = fr.ReadLine();
                ss = string.Concat(ss, "\n");
                fw.Write(ss);

                int nrec = int.Parse(words[2]);
                for (int j = 0; j < nrec; ++j)
                {
                    ss = fr.ReadLine();
                    string s2 = string.Concat("Shape:", j.ToString(),", MultiPointZ nVertices=1, nParts=0", "\n");
                    fw.Write(s2);
                    ss = fr.ReadLine();
                    words = ss.Split(delimiterChars);
                    s2 = string.Concat("Bounds: ", words[1]," , ",words[2]," , ", words[1]," , ",words[2], "\n");
                    fw.Write(s2);
                    s2 = string.Concat("  ", words[1], " , ", words[2],  "\n");
                    fw.Write(s2);
                }

                fr.Close();
                fw.Close();

            }

            return (0);
        }

        public int convertDxf_txt(string file_r)
        {

            /*
                        if (File.Exists(file_w))
                        {
                            File.Delete(file_w);
                        }
              */
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            //           StreamWriter fs = new StreamWriter(file_w);
            /*
            if (File.Exists(file_r))
            {
                int icomp = 1;
                using (BinaryReader reader = new BinaryReader(File.Open(file_r, FileMode.Open)))
                {
                    byte bt = reader.ReadByte();        //  numero versione
                    byte[] bts = reader.ReadBytes(3);       //data ultima modifica
                    int numRec = reader.ReadInt32();
                    string ss = "numero records: ";
                    ss = string.Concat(ss, numRec.ToString(), "\n");
                    fs.Write(ss);
                    int lengthHeader = reader.ReadInt16();  //  length of header structure
                    int lengthRec = reader.ReadInt16();  //  length of each record + 1
                    reader.ReadBytes(20);
                    int no_fields = (lengthHeader - 33) / 32;
                    for (int i = 0; i < no_fields; ++i)
                    {
                        int j;
                        bts = reader.ReadBytes(11);
                        for (j = 0; j < 11; ++j)
                        {
                            if (bts[j] == 0) break;
                        }
                        byte[] bte = new byte[j];
                        Buffer.BlockCopy(bts, 0, bte, 0, j);

                        ss = System.Text.Encoding.UTF7.GetString(bte);

                        //           MessageBox.Show(j.ToString() + "  " + ss.Length.ToString());

                        vecT[i].name = string.Copy(ss);

                        if (ifile == 1 && i == 0)
                        {
                            icomp = string.Compare(vecT[i].name, "ID");
                            if (icomp == 0) ifile = 5;
                        }

                        bt = reader.ReadByte();
                        vecT[i].type = (char)bt;
                        int fieldDataAddress = reader.ReadInt32();
                        byte fieldlength = reader.ReadByte();
                        vecT[i].length = fieldlength;
                        byte decimalCount = reader.ReadByte();
                        vecT[i].dec = decimalCount;

                        reader.ReadBytes(14);
                    }
                    reader.ReadBytes(1);
                }
            }
             * */

            return (0);
        }

        public void convert_shp3(string fileName, string target)
        {
            int ipos = fileName.IndexOf('.');
            string ss = fileName.Substring(0, ipos);
            string file_r = string.Concat(ss, ".shp");
            string file_rx = string.Concat(ss, ".shx");

            string path = FormIniz.quadroUnione5000;
            string shp = string.Concat(path, target, "_shp.txtx");

            int iret = convertShp3_txt(file_r, file_rx, shp);
            if (iret != 0) MessageBox.Show("errore:  convertShp_txt  " + iret.ToString());


        }

        private int convertShp3_txt(string file_r, string file_rx, string file_w)
        {

            if (File.Exists(file_w))
            {
                File.Delete(file_w);
            }
            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            int iline = 0;
            int iret = 0;
            using (BinaryReader reader = new BinaryReader(File.Open(file_rx, FileMode.Open)))
            {
                byte[] bytes = reader.ReadBytes(4);
                Array.Reverse(bytes);
                int fileCode = BitConverter.ToInt32(bytes, 0);
                if (fileCode != 9994) return 1;

                byte[] skip = reader.ReadBytes(20);

                bytes = reader.ReadBytes(4);
                Array.Reverse(bytes);
                int fileLength = BitConverter.ToInt32(bytes, 0);
                int nRec = (fileLength - 50) / 4;

                int version = reader.ReadInt32();
                if (version != 1000) return 2;

                skip = reader.ReadBytes(68);
  //  MessageBox.Show("****   " +file_r + "  " + nRec.ToString() + "   " + fileLength.ToString());

                for (int irec = 0; irec < nRec; ++irec)   //
                {
                    bytes = reader.ReadBytes(4);
                    //               Array.Reverse(bytes);
                    //             int offset = BitConverter.ToInt32(bytes, 0);

                    bytes = reader.ReadBytes(4);
                    Array.Reverse(bytes);
                    int contentLength = BitConverter.ToInt32(bytes, 0);
                    int nPoints = (contentLength - 40) / 16;
                    if (nPoints < 0) nPoints = 0;
                    iline += nPoints;
   //                 if (irec < 3) MessageBox.Show(contentLength.ToString() + "  " + nPoints.ToString()  + "  " + iline.ToString());
                }
            }
            StreamWriter fs = new StreamWriter(file_w);
            if (File.Exists(file_r))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(file_rx, FileMode.Open)))
                {
                    byte[] bytes = reader.ReadBytes(4);
                    Array.Reverse(bytes);
                    int fileCode = BitConverter.ToInt32(bytes, 0);
                    if (fileCode != 9994) return 1;

                    byte[] skip = reader.ReadBytes(20);

                    bytes = reader.ReadBytes(4);
                    Array.Reverse(bytes);
                    int fileLength = BitConverter.ToInt32(bytes, 0);

                    int nRec = (fileLength - 50) / 4;

                    int version = reader.ReadInt32();
                    if (version != 1000) return 2;

                    int nSHPType = reader.ReadInt32();
                    double Xmin = reader.ReadDouble();
                    double Ymin = reader.ReadDouble();
                    double Xmax = reader.ReadDouble();
                    double Ymax = reader.ReadDouble();
                    double Zmin = reader.ReadDouble();
                    double Zmax = reader.ReadDouble();
                    double Mmin = reader.ReadDouble();
                    double Mmax = reader.ReadDouble();

                    string ss = SHPTypeName(nSHPType);
                    if (ss.Contains("UnknownShapeType")) return 3;

                    ss = "Shapefile type: ";
                    ss = string.Concat(ss, SHPTypeName(nSHPType), "  # of Shape: ", nRec.ToString(), "; # of Points: ", iline.ToString());
                    fs.WriteLine(ss);

                    ss = "File Bounds: ";
                    string s1 = Xmin.ToString("F0", nfi);
                    ss = string.Concat(ss, s1, " , ");

                    s1 = Ymin.ToString("F0", nfi);
                    ss = string.Concat(ss, s1, " , ");

                    s1 = Xmax.ToString("F0", nfi);
                    ss = string.Concat(ss, s1, " , ");

                    s1 = Ymax.ToString("F0", nfi);
                    ss = string.Concat(ss, s1, "  ");
                    fs.WriteLine(ss);

                    BinaryReader readerp = new BinaryReader(File.Open(file_r, FileMode.Open));
                    for (int irec = 0; irec < nRec; ++irec)   //
                    {


                        bytes = reader.ReadBytes(4);
                        Array.Reverse(bytes);
                        int offset = BitConverter.ToInt32(bytes, 0);

                        bytes = reader.ReadBytes(4);
                        Array.Reverse(bytes);
                        int contentLength = BitConverter.ToInt32(bytes, 0);

                        byte[] vect = new byte[contentLength];

                        readerp.BaseStream.Position = 2 * offset;

                        bytes = readerp.ReadBytes(4);
                        Array.Reverse(bytes);
                        int rec = BitConverter.ToInt32(bytes, 0);

                        bytes = readerp.ReadBytes(4);
                        Array.Reverse(bytes);
                        int length = BitConverter.ToInt32(bytes, 0);

                        int type = readerp.ReadInt32();
 //           if(irec < 3)       MessageBox.Show(offset.ToString() + "  " + contentLength.ToString() + "  " + nRec.ToString()+ "  " +type.ToString());
                    int ipnt = 4;
                        if (type == SHPT_ARCZ || type == SHPT_POLYGONZ)
                        {
                            Xmin = readerp.ReadDouble();
                            Ymin = readerp.ReadDouble();
                            Xmax = readerp.ReadDouble();
                            Ymax = readerp.ReadDouble();
                            int nParts = readerp.ReadInt32();
                            int nPoints = readerp.ReadInt32();
                            ipnt += 40;

                            ss = "Shape:" + irec.ToString() + ", ";
                            ss = string.Concat(ss, SHPTypeName(type));
                            ss = string.Concat(ss, "  nVertices=", nPoints.ToString(), ", nParts=", nParts.ToString());
                            fs.WriteLine(ss);

                            ss = " Bounds: ";
                            s1 = Xmin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Xmax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, "  ");
                            fs.WriteLine(ss);

                            int[] iPart = new int[nParts];
                            for (int j = 0; j < nParts; ++j)
                            {
                                iPart[j] = readerp.ReadInt32();
                                ipnt += 4;
                                //                            if (j > 0) MessageBox.Show(iPart[j].ToString() + "  " + irec.ToString());
                            }
                            if (nParts > 1)
                            {
                                ss = " nParts: ";
                                for (int j = 0; j < nParts; ++j)
                                {
                                    ss = string.Concat(ss, iPart[j], ", ");
                                }
                                fs.WriteLine(ss);
                            }

                            Point[] iPoint = new Point[nPoints];

                            for (int j = 0; j < nPoints; ++j)
                            {
                                iPoint[j].X = (int)readerp.ReadDouble();
                                iPoint[j].Y = (int)readerp.ReadDouble();
                                ss = "    ";
                                ss = string.Concat(ss, iPoint[j].X.ToString() + " ,   " + iPoint[j].Y.ToString() + " ,   ");
                                fs.WriteLine(ss);
                                ipnt += 16;
                            }

                            continue;
                        }

                        if (type == SHPT_MULTIPOINTZ)
                        {
                            Xmin = readerp.ReadDouble();
                            Ymin = readerp.ReadDouble();
                            Xmax = readerp.ReadDouble();
                            Ymax = readerp.ReadDouble();
                            int nPoints = readerp.ReadInt32();
                            int nParts = 0;

                            ss = "Shape:" + irec.ToString() + ", ";
                            ss = string.Concat(ss, SHPTypeName(type));
                            ss = string.Concat(ss, "  nVertices=", nPoints.ToString(), ", nParts=", nParts.ToString());
                            fs.WriteLine(ss);

                            ss = " Bounds: ";
                            s1 = Xmin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Xmax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, "  ");
                            fs.WriteLine(ss);

                            Point[] iPoint = new Point[nPoints];

                            for (int j = 0; j < nPoints; ++j)
                            {
                                iPoint[j].X = (int)readerp.ReadDouble();
                                iPoint[j].Y = (int)readerp.ReadDouble();
                                ss = "    ";
                                ss = string.Concat(ss, iPoint[j].X.ToString() + " ,   " + iPoint[j].Y.ToString() + " ,   ");
                                fs.WriteLine(ss);
                                ipnt += 16;
                            }
                            continue;
                        }

                        if (type == SHPT_MULTIPOINTZ)       //   type == 18
                        {
                            Xmin = readerp.ReadDouble();
                            Ymin = readerp.ReadDouble();
                            Xmax = readerp.ReadDouble();
                            Ymax = readerp.ReadDouble();
                            int nPoints = readerp.ReadInt32();
                            int nParts = 0;
                            ipnt += 36;

                            ss = "Shape:" + irec.ToString() + ", ";
                            ss = string.Concat(ss, SHPTypeName(type));
                            ss = string.Concat(ss, "  nVertices=", nPoints.ToString(), ", nParts=", nParts.ToString());
                            fs.WriteLine(ss);

                            ss = " Bounds: ";
                            s1 = Xmin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Xmax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, "  ");
                            fs.WriteLine(ss);

                            Point[] iPoint = new Point[nPoints];

                            for (int j = 0; j < nPoints; ++j)
                            {
                                iPoint[j].X = (int)readerp.ReadDouble();
                                iPoint[j].Y = (int)readerp.ReadDouble();
                                ss = "    ";
                                ss = string.Concat(ss, iPoint[j].X.ToString() + " ,   " + iPoint[j].Y.ToString() + " ,   ");
                                fs.WriteLine(ss);
                                ipnt += 16;
                            }
                            continue;
                        }
                       
                        if (type == SHPT_POINTZ)                //  type == 11
                        {
                            Xmin = readerp.ReadDouble();
                            Ymin = readerp.ReadDouble();
                            Xmax = readerp.ReadDouble();
                            Ymax = 0.0;
            //               Ymax = readerp.ReadDouble();
          //                   int nPoints = readerp.ReadInt32();
                            int nPoints = 1;
                            int nParts = 0;
                         ipnt += 16;
                          
                            ss = "Shape:" + irec.ToString() + ", ";
                            ss = string.Concat(ss, SHPTypeName(type));
                            ss = string.Concat(ss, "  nVertices=", nPoints.ToString(), ", nParts=", nParts.ToString());
                            fs.WriteLine(ss);

                            ss = " Bounds: ";
                            s1 = Xmin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymin.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Xmax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, " , ");

                            s1 = Ymax.ToString("F0", nfi);
                            ss = string.Concat(ss, s1, "  ");
                            fs.WriteLine(ss);


                            continue;
                        }

                       
                        //                MessageBox.Show(ipnt.ToString() + "  " + contentLength.ToString() + "  " + length.ToString());

                        MessageBox.Show("opzione non prevista");
                        iret = 1;
                        break;
                    }






                    reader.Close();
                    readerp.Close();
                }

                fs.Close();
            }

            return (iret);
        }

        private int countPoints(string file_r)
        {
            int iret = 0;
            int nPoint = 0;

            char[] delimiterChars = { ':', ',', ';' };
            string[] words;

            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);


                string ss = fr.ReadLine();
                words = ss.Split(delimiterChars);

                int nShape = int.Parse(words[2]);
                nShapeT = nShape; 

                ss = fr.ReadLine();

                int ipos = 0;
                int lpos = 0;
                string s1 = string.Empty;
                for (int i = 0; i < nShape; ++i)
                {
                    ss = fr.ReadLine();

                    int len = ss.Length;
                    ipos = ss.IndexOf('=', 20, len - 20);
                    lpos = 0;
                    if (ipos > 0)
                    {
                        ++ipos;
                        lpos = ss.IndexOf(',', ipos, len - ipos);
                        s1 = ss.Substring(ipos, lpos - ipos);
                        int icount = int.Parse(s1);
                        for (int j = 0; j <= icount; ++j)
                        {
                            ss = fr.ReadLine();
                        }
                        nPoint += icount;
                    }

                    else
                    {
                        MessageBox.Show("countPoints  " + file_r + "  errore");
                        iret = -1;
                        break;

                    }
                }
               
                fr.Close();
            }
            
            if (iret < 0) return (iret);
            else nPoints = nPoint;
             
            return (iret);
            
        }


        private int addfile_shp(string file_r, string file_w)
        {
            int nPoint = 0;
            int iret = 0;

            char[] delimiterChars = { ':', ',', ';' };
            string[] words;


            if (File.Exists(file_r))
            {
                StreamReader fr = File.OpenText(file_r);
                StreamWriter fw = File.AppendText(file_w);

                string ss = fr.ReadLine();
                words = ss.Split(delimiterChars);
                ss = string.Concat(ss, "\n");
                fw.Write(ss);

                int nShape = int.Parse(words[2]);
                nShapeT += nShape;
   
                ss = fr.ReadLine();
                ss = string.Concat(ss, "\n");
                fw.Write(ss);

                int ipos = 0;
                int lpos = 0;
                string s1 = string.Empty;
                for (int i = 0; i < nShape; ++i)
                {
                    ss = fr.ReadLine();
                    ss = string.Concat(ss, "\n");
                    fw.Write(ss);

                    int len = ss.Length;
                    ipos = ss.IndexOf('=', 20, len - 20);
                    lpos = 0;
                    if (ipos > 0)
                    {
                        ++ipos;
                        lpos = ss.IndexOf(',', ipos, len - ipos);
                        s1 = ss.Substring(ipos, lpos - ipos);
                        int icount = int.Parse(s1);
                        for (int j = 0; j <= icount; ++j)
                        {
                            ss = fr.ReadLine();
                            ss = string.Concat(ss, "\n");
                            fw.Write(ss);
                        }
                        nPoint += icount;
                    }

                    else
                    {
                        MessageBox.Show("linea2_shp  " + file_r + "  errore");
                        nPoint = -1;
                        break;
                    }
                }
                fr.Close();
                fw.Close();
            }
            nPoints += nPoint;

            return (iret);

        }

        private int addfile1_shp(int nfile,string file_r, string file_w)
        {

            int iret = 0;

            char[] delimiterChars = { ':', ',', ';' };
            string[] words;

            StreamReader fr = File.OpenText(file_r);
            StreamWriter fw = new StreamWriter(file_w);

            string ss = fr.ReadLine();
            int mpos = ss.IndexOf('#');
            string ss1 = ss.Substring(0, mpos + 1);
            ss1 = string.Concat(ss1, " of Shape: ", nShapeT.ToString(), "; # of Points: ", nPoints.ToString(), "\n");
            fw.Write(ss1);

            words = ss.Split(delimiterChars);
            int nShape = int.Parse(words[2]);

            ss = fr.ReadLine();
            ss = string.Concat(ss, "\n");
            fw.Write(ss);

            for (int i = 0; i < nShape; ++i)
            {

                ss = fr.ReadLine();
                ss = string.Concat(ss, "\n");
                fw.Write(ss);

                int len = ss.Length;
                int ipos = ss.IndexOf('=', 20, len - 20);
                int lpos = 0;
                if (ipos > 0)
                {
                    ++ipos;
                    lpos = ss.IndexOf(',', ipos, len - ipos);
                    string s1 = ss.Substring(ipos, lpos - ipos);
                    int icount = int.Parse(s1);
                    for (int j = 0; j <= icount; ++j)
                    {
                        ss = fr.ReadLine();
                        ss = string.Concat(ss, "\n");
                        fw.Write(ss);
                    }
                }
            }

            int count = nShape;
            for (int k = 1; k < nfile; ++k)
            {
                ss = fr.ReadLine();
                words = ss.Split(delimiterChars);
                nShape = int.Parse(words[2]);
                ss = fr.ReadLine();

                for (int i = 0; i < nShape; ++i)
                {
                    string ss2 = fr.ReadLine();
                    words = ss2.Split(delimiterChars);
                    ss = string.Concat(words[0], ":", count.ToString(), ",", words[2], ",", words[3], "\n");
                    fw.Write(ss);

                    int len = ss.Length;
                    int ipos = ss.IndexOf('=', 20, len - 20);
                    int lpos = 0;
                    if (ipos > 0)
                    {
                        ++ipos;
                        lpos = ss.IndexOf(',', ipos, len - ipos);
                        string s1 = ss.Substring(ipos, lpos - ipos);
                        int icount = int.Parse(s1);
                        for (int j = 0; j <= icount; ++j)
                        {
                            ss = fr.ReadLine();
                            ss = string.Concat(ss, "\n");
                            fw.Write(ss);
                        }
                        ++count;
                    }
                }
            }


            fr.Close();
            fw.Close();

            if (count != nShapeT)
            {
                MessageBox.Show("addfile1_shp_shp: errore  ", count.ToString() + "  " + nShapeT.ToString());
                iret = -1;
            }

            return (iret);
        }
    


    }
}
